<template>
  <div class="login-container" :class="{'is-mobile': isMobile}">
    <!-- 背景部分 - 桌面端显示 -->
    <div class="login-background" v-if="!isMobile">
      <div class="background-content">
        <h1 class="app-title">企业管理系统</h1>
        <p class="app-description">高效、安全、智能的企业管理平台</p>
      </div>
    </div>
    
    <!-- 登录卡片 -->
    <div class="login-card">
      <!-- 移动端标题 -->
      <div class="mobile-title" v-if="isMobile">
        <h1 class="app-title">企业管理系统</h1>
      </div>
      
      <el-form 
        ref="loginFormRef" 
        :model="loginForm" 
        :rules="loginRules" 
        label-width="0px"
        class="login-form"
      >
        <h3 class="login-title">用户登录</h3>
        
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username" 
            placeholder="用户名" 
            name="username" 
            type="text"
            prefix-icon="el-icon-user"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input 
            v-model="loginForm.password" 
            placeholder="密码" 
            name="password" 
            :type="showPwd ? 'text' : 'password'"
            prefix-icon="el-icon-lock"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          >
          </el-input>
        </el-form-item>
        
        <el-form-item>
          <el-button 
            :loading="loading" 
            type="primary" 
            style="width:100%;margin-bottom:30px;"
            @click.native.prevent="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456',
        rememberMe: false
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
        ]
      },
      showPwd: false,
      loading: false,
      isMobile: false
    }
  },
  mounted() {
    this.handleResize();
    window.addEventListener('resize', this.handleResize);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize);
  },
  methods: {
    handleResize() {
      this.isMobile = window.innerWidth <= 768;
    },
    showPassword() {
      this.showPwd = !this.showPwd;
    },
    handleLogin() {
      this.$refs.loginFormRef.validate((valid) => {
        if (valid) {
          this.loading = true;
          
          // 模拟登录请求
          setTimeout(() => {
            this.loading = false;
            this.$message({
              message: '登录成功',
              type: 'success',
              duration: 2000
            });
            
            // 登录成功后跳转到首页
            this.$router.push('/user');
          }, 1500);
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
  position: relative;
  padding: 20px;
  transition: all 0.3s ease;
}

.login-background {
  position: absolute;
  left: 0;
  top: 0;
  width: 50%;
  height: 100%;
  background: linear-gradient(135deg, #3498db 0%, #2980b9 100%);
  color: #fff;
  border-radius: 0 10px 10px 0;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.background-content {
  text-align: center;
  padding: 0 40px;
  max-width: 400px;
}

.app-title {
  font-size: 36px;
  font-weight: 700;
  margin-bottom: 15px;
  animation: fadeInUp 1s ease forwards;
  opacity: 0;
  transform: translateY(20px);
}

.app-description {
  font-size: 16px;
  line-height: 1.6;
  margin-bottom: 0;
  animation: fadeInUp 1s ease 0.3s forwards;
  opacity: 0;
  transform: translateY(20px);
}

.login-card {
  width: 100%;
  max-width: 400px;
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  padding: 40px;
  position: relative;
  z-index: 1;
  transition: all 0.3s ease;
}

.mobile-title {
  text-align: center;
  margin-bottom: 30px;
}

.login-title {
  text-align: center;
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 30px;
  color: #333;
}

.login-form {
  margin-top: 20px;
}

.el-input {
  height: 48px;
  font-size: 16px;
  border-radius: 8px;
  transition: border-color 0.3s ease;
}

.el-input:focus {
  border-color: #409EFF;
  box-shadow: 0 0 0 3px rgba(64, 158, 255, 0.2);
}

.show-pwd {
  cursor: pointer;
  color: #889aa4;
  transition: color 0.3s ease;
}

.show-pwd.active {
  color: #409EFF;
}

.remember-me {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.forget-pwd {
  font-size: 14px;
  color: #889aa4;
  transition: color 0.3s ease;
}

.forget-pwd:hover {
  color: #409EFF;
}

.el-button {
  height: 48px;
  font-size: 16px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.el-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

.login-extra {
  text-align: center;
  font-size: 14px;
  color: #667085;
}

.login-extra a {
  color: #409EFF;
  text-decoration: none;
  transition: color 0.3s ease;
}

.login-extra a:hover {
  color: #2c80e0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    padding: 10px;
  }
  
  .login-background {
    display: none;
  }
  
  .login-card {
    max-width: 100%;
    padding: 30px;
    box-shadow: none;
    border-radius: 0;
  }
}

/* 动画效果 */
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>